package com.RSA;

import java.util.Map;

public class RSATester {

    static String publicKey;
    static String privateKey;

    static {
        try {
            Map<String, Object> keyMap = RSAUtils.genKeyPair();
            publicKey = RSAUtils.getPublicKey(keyMap);
            privateKey = RSAUtils.getPrivateKey(keyMap);
            System.err.println("公钥: \n\r" + publicKey);
            System.err.println("私钥： \n\r" + privateKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        //test();
        testSign();
    }

    static void test() throws Exception {
        System.err.println("公钥加密——私钥解密");
        String source = "123456";
        System.out.println("\r加密前文字：\r\n" + source);
        byte[] data = source.getBytes();
        byte[] encodedData = RSAUtils.encryptByPublicKey(data, publicKey);
        System.out.println("加密后文字：\r\n" + new String(encodedData));
        byte[] decodedData = RSAUtils.decryptByPrivateKey(encodedData, privateKey);
        String target = new String(decodedData);
        System.out.println("解密后文字: \r\n" + target);
    }

    static void testSign() throws Exception {

//    	String prikeyString = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIfh0X9jI7OlueA9rTiyDIsEBaLH88wAvqmJdSMTzkZdPZa1WBRPYUnSG0RIkR9axJFgDLqJ9gxz8nXlOHR51K5iSHxplvLYdKGmNJ7lY4NYsuvL40ZyI+aM1ATSW1vhbNKa9AX7LvasUY1RZ/KzKntCc1gxMNywkOQL7DwTkV/HAgMBAAECgYA5CcudA4qbAHgT7w/leqfVOuz6taBbA/0EwZSQEuJFmyIdPyYn8GJQUIftk+CHHtzggUcWjo7gD1O5tWyLSBDEO3KMVRcDqdRjj349pZWutezWLYqka0CtAdveV+RjN7DoSVrTQsDBuUOZAPCWQFrgC5vfDYCbFTqZJcOwSIC4kQJBAOA6fWkgYy+fxd0qaIM1aXHJWEP9DqlfwLuIR9hhdST+bbxAE/Qa5w9eTuh17po8xSJO+MqfFV/mYEv8eKSEl8MCQQCbIrZwdD8QcNPusVBPky1RPl5ylOLqy9m15EaEXSVJzWfO33QDgTsp4dIt+DicKm/On/cD25FAH505tAn4IdutAkA4SDYmZwy+bXo602Q8WvHGr+VJ24EMno5ABCsUcxH2YS6mK+dWRFJvFxrzWnaznEFXJT9pjJjO44KUUCDwGqAFAkB2KcSIdyKp5SaoAorHcbTs0vKhl+IXMVoMf2lu3HvQcRIGspDtMnvb9NNgjFaiLHipC5Lcb1t9WhcpooaNZ0ypAkB0Nbz7ZYv3vnPdl8Lko1C6sZwUN55214XRjGfBYNHVJ1aep1qGC7/Z6vz5hyfdXbuNXNIYyBdcz990mFPVi8J4";
//    	String pubKeyString = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCH4dF/YyOzpbngPa04sgyLBAWix/PMAL6piXUjE85GXT2WtVgUT2FJ0htESJEfWsSRYAy6ifYMc/J15Th0edSuYkh8aZby2HShpjSe5WODWLLry+NGciPmjNQE0ltb4WzSmvQF+y72rFGNUWfysyp7QnNYMTDcsJDkC+w8E5FfxwIDAQAB";
    	System.err.println("私钥加密——公钥解密");
        String source = "123456";
        System.out.println("原文字：\r\n" + source);
        byte[] data = source.getBytes();
        byte[] encodedData = RSAUtils.encryptByPrivateKey(data, publicKey);
        System.out.println("加密后：\r\n" + new String(encodedData));
        byte[] decodedData = RSAUtils.decryptByPublicKey(encodedData, publicKey);
        String target = new String(decodedData);
        System.out.println("解密后: \r\n" + target);
        System.err.println("私钥签名——公钥验证签名");
        String sign = RSAUtils.sign(data, privateKey);
        System.err.println("签名:\r" + sign);
        boolean status = RSAUtils.verify(encodedData, privateKey, sign);
        System.err.println("验证结果:\r" + status);
    }

}
